package net.pessu.android.shoppoid.calendar;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import net.pessu.android.shoppoid.R;
import net.pessu.android.shoppoid.gui.IhmObjets;
import net.pessu.android.shoppoid.util.DateUtils;
import net.pessu.android.shoppoid.util.PropertiesManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class DateDesSoldes extends GDActivity {

	private static final String NON = "Non";

	private static final String ESPACE = " ";

	private static final String PROBLEME_DE_PARSAGE_DE_DATES = "Probleme de parsage de dates";

	private static final String IMPOSSIBLE_D_OUVRIR_LE_FICHIER_PROPERTIES = "impossible d'ouvrir le fichier properties";

	private static final String DATE_DES_SOLDES = "DateDesSoldes";

	private static final String NOM = "nom";

	private static final String OUI = "Oui";

	private static final String CHOISIR_UN_DEPARTEMENT = IhmObjets
			.getStringContext(R.string.choosedep);

	Context currentContext;
	TextView departement;
	TextView dateText;
	PropertiesManager properties;
	boolean isDepRecorded = false;

	DateDesSoldesServices services;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.calendrier);
		ActionBar bar = getActionBar();
		bar.setTitle(getString(R.string.datesolde));
		bar.setFont(IhmObjets.tfTitre);
		bar.setFontSize(30);
		services = DateDesSoldesServices.getInstance(this);

		currentContext = this;

		departement = (TextView) findViewById(R.id.departementText);
		dateText = (TextView) findViewById(R.id.dateText);

		try {
			properties = new PropertiesManager(getAssets().open(
					"propertiesdates/062012.properties"));

			Toast.makeText(getApplicationContext(),
					IhmObjets.getStringContext(R.string.chooseadepartement),
					Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Log.w(DATE_DES_SOLDES, IMPOSSIBLE_D_OUVRIR_LE_FICHIER_PROPERTIES);
			Toast.makeText(getApplicationContext(),
					IhmObjets.getStringContext(R.string.nodateavaible),
					Toast.LENGTH_SHORT).show();
		}

		String dep = services.getDepartement();
		if (dep != null) {
			isDepRecorded = true;
			departement.setText(dep);
			dateText.setText(formaterAffichageDate(
					properties.getProperties(NOM),
					properties.getProperties(dep.split(ESPACE)[0])));
		}

		departement.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						currentContext);
				builder.setTitle(CHOISIR_UN_DEPARTEMENT);
				builder.setSingleChoiceItems(ConstanteDateDesSoldes.items, -1,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int item) {
								Toast.makeText(getApplicationContext(),
										ConstanteDateDesSoldes.items[item],
										Toast.LENGTH_SHORT).show();
								departement
										.setText(ConstanteDateDesSoldes.items[item]);
								dialog.cancel();
								String numDep = ConstanteDateDesSoldes.items[item]
										.toString().split(ESPACE)[0];
								dateText.setText(formaterAffichageDate(
										properties.getProperties(NOM),
										properties.getProperties(numDep)));
								if (!isDepRecorded) {
									services.setDepartement(ConstanteDateDesSoldes.items[item]
											.toString());
								} else {
									ajouterBoiteDeDialogue();
								}
							}
						});
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}

	private String formaterAffichageDate(String nom, String chaine) {
		StringBuilder sb = new StringBuilder();
		String[] tblChaine = chaine.split("-");

		Date dateDebut = null;
		Date dateFin = null;
		try {
			dateDebut = IhmObjets.sdf_properties.parse(tblChaine[0]);
			dateFin = IhmObjets.sdf_properties.parse(tblChaine[1]);
		} catch (ParseException e) {
			Log.w(DATE_DES_SOLDES, PROBLEME_DE_PARSAGE_DE_DATES);
		}

		if (dateDebut != null && dateFin != null) {

			Date today = new Date();

			sb.append(IhmObjets.getStringContext(R.string.du));
			sb.append(ESPACE);
			sb.append(IhmObjets.sdf_jj_mmmm_aaaa.format(dateDebut));
			sb.append(ESPACE);
			sb.append(IhmObjets.getStringContext(R.string.au));
			sb.append(ESPACE);
			sb.append(IhmObjets.sdf_jj_mmmm_aaaa.format(dateFin));

			if (today.before(dateDebut)) {
				sb.append(IhmObjets.getStringContext(R.string.ilreste));
				sb.append(ESPACE);
				sb.append(IhmObjets.dfEntier.format(DateUtils
						.getDaysBetweenDates(today, dateDebut)));
				sb.append(IhmObjets.getStringContext(R.string.joursavant));
				sb.append(ESPACE);
				sb.append(nom);
			}
		} else {
			sb.append(IhmObjets.getStringContext(R.string.nodatefordep));
		}
		return sb.toString();
	}

	public void ajouterBoiteDeDialogue() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(
				IhmObjets.getStringContext(R.string.voulezvousdep) + " "
						+ departement.getText().toString())
				.setCancelable(false)
				.setPositiveButton(OUI, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						services.editDepartement(departement.getText()
								.toString());
					}
				})
				.setNegativeButton(NON, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
